home *** CD-ROM | disk | FTP | other *** search
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "QDContext.h"
- #include "WindowManager.h"
- #include "BaseDialog.h"
-
-
-
-
-
- extern WindowManager *gWindowManager;
-
-
- enum
- {
- kOKButtonID = 1,
- kCancelButtonID = 2
- };
-
-
-
-
-
- BaseDialog::BaseDialog(UInt32 dialogID,Boolean isModal)
- {
- fDialog = ::GetNewDialog(dialogID,NULL,(WindowPtr)-1L);
- if (fDialog != NULL)
- {
- fFlags = isModal ? (kDialog | kModal) : kDialog;
- fWindow = (WindowPtr)fDialog;
- gWindowManager->DoAddWindow(this);
- SelectWindow(fWindow);
- }
- }
-
-
-
-
-
- BaseDialog::~BaseDialog(void)
- {
- if (fDialog != NULL)
- {
- gWindowManager->DoDeleteWindow(this);
- DisposeDialog(fDialog);
- fDialog = NULL;
- fWindow = NULL;
- }
- }
-
-
-
-
-
- void BaseDialog::DoDialogEvent(EventRecord *event)
- {
- QDContext context(fWindow);
-
-
- // We don't do any special preprocessing of
- // events (yet) so just pass it straight thru
- HandleDialogEvent(event);
- }
-
-
- #if 0
- #pragma mark -
- #endif
-
-
- void BaseDialog::HandleDialogEvent(EventRecord *event)
- {
- DialogPtr dialog;
- short item;
-
-
- if (HandleDialogEventFilter(event))
- {
- if (DialogSelect(event,&dialog,&item))
- HandleDialogItemhit(item);
-
- // We have to check here to see if we've been deleted
- // because HandleDialogItemhit might have been a hit to
- // the OK or Cancel button.
- if (fDialog != NULL)
- {
- // Automagically handle the hiliting
- // and updating of the OK button
- switch(event->what)
- {
- case keyDown:
- case keyUp:
- case mouseDown:
- case mouseUp:
- // Only query the OK button state if something
- // happened that could have possibly changed it
- SetOKState(HandleOKButtonHiliteQuery());
- break;
-
- case updateEvt:
- DrawThickOutline(kOKButtonID);
- break;
- }
- }
- }
- }
-
-
-
-
-
- Boolean BaseDialog::HandleDialogEventFilter(EventRecord *event)
- {
- ControlHandle control;
- short type;
- Rect box;
-
-
- // Filter out command/option/control keys
- if (event->what == keyDown && (event->modifiers & (cmdKey | optionKey | controlKey)))
- return false;
-
- switch(event->what)
- {
- case keyDown:
- case autoKey:
- switch(charCodeMask & event->message)
- {
- case 0x0D: // RETURN
- GetDialogItem(fDialog,kOKButtonID,&type,(Handle*)&control,&box);
- if ((type == 4) && (control[0]->contrlHilite == 0))
- {
- AnimateButtonPress(kOKButtonID);
- HandleDialogItemhit(kOKButtonID);
- return false;
- }
- break;
-
- case 0x1B: // ESC
- GetDialogItem(fDialog,kCancelButtonID,&type,(Handle*)&control,&box);
- if ((type == 4) && (control[0]->contrlHilite == 0))
- {
- AnimateButtonPress(kCancelButtonID);
- HandleDialogItemhit(kCancelButtonID);
- return false;
- }
- break;
- }
- break;
- }
-
- // Continue processing event
- return true;
- }
-
-
-
-
-
- void BaseDialog::HandleDialogItemhit(short item)
- {
- // Default behavior for OK and Cancel
- // button hits are to dismiss the dialog
- switch(item)
- {
- case kOKButtonID:
- case kCancelButtonID:
- delete this;
- break;
- }
- }
-
-
-
-
-
- Boolean BaseDialog::HandleOKButtonHiliteQuery(void)
- {
- // Default behavior is to have a
- // hilited and active OK button
- return true;
- }
-
-
- #if 0
- #pragma mark -
- #endif
-
-
- void BaseDialog::DrawThickOutline(short item)
- {
- QDContext context(fWindow);
- ControlHandle control;
- short type;
- Rect box;
-
-
- // Draw a thick border around an item (default button outline)
- GetDialogItem(fDialog,item,&type,(Handle*)&control,&box);
- if (type == 4)
- {
- InsetRect(&box,-4,-4);
-
- if (control[0]->contrlHilite == 255)
- PenPat(&qd.gray);
-
- PenSize(3,3);
- FrameRoundRect(&box,16,16);
- }
- }
-
-
-
-
-
- void BaseDialog::SetOKState(Boolean isActive)
- {
- ControlHandle control;
- short type;
- Rect box;
-
-
- // (De)Hilite the OK button to show its (in)active state
- GetDialogItem(fDialog,kOKButtonID,&type,(Handle*)&control,&box);
- if (type == 4)
- {
- if (isActive)
- {
- if (control[0]->contrlHilite == 255)
- {
- HiliteControl(control,0);
- DrawThickOutline(kOKButtonID);
- }
- }
- else
- {
- if (control[0]->contrlHilite != 255)
- {
- HiliteControl(control,255);
- DrawThickOutline(kOKButtonID);
- }
- }
- }
- }
-
-
-
-
-
- void BaseDialog::AnimateButtonPress(short item)
- {
- ControlHandle control;
- UInt32 ticks;
- short type;
- Rect box;
-
-
- // Make it appear to the user that a button was pressed
- GetDialogItem(fDialog,item,&type,(Handle*)&control,&box);
- if ((type == 4) && (control[0]->contrlHilite == 0))
- {
- HiliteControl(control,kControlButtonPart);
- Delay(10,&ticks);
- HiliteControl(control,0);
- }
- }
-
-
-
-
-
- void BaseDialog::GetItemText(short item,char *text)
- {
- Handle data;
- short type;
- Rect rect;
- Str255 pstr;
-
-
- // Get the text of a static text item or an
- // edit text item and return it as a c string
- GetDialogItem(fDialog,item,&type,&data,&rect);
- if ((type == statText) || (type == editText))
- GetDialogItemText(data,pstr);
- else
- pstr[0] = '\0';
-
- pstr[1 + pstr[0]] = '\0';
- strcpy(text,(char*)&pstr[1]);
- }
-
-
-
-
-
- int BaseDialog::GetItemTextAsDecimal(short item)
- {
- char text[256];
-
-
- // Get the text of a static text item or an edit
- // text item and return it as a decimal value
- GetItemText(item,text);
- return atoi(text);
- }
-
-
-
-
-
- void BaseDialog::SetItemText(short item,char *text)
- {
- Handle data;
- short type;
- Rect rect;
- Str255 pstr;
-
-
- // Set the text of a static text item
- // or an edit text item from a c string
- GetDialogItem(fDialog,item,&type,&data,&rect);
- if ((type == statText) || (type == editText))
- {
- pstr[0] = strlen(text);
- strcpy((char*)&pstr[1],text);
- SetDialogItemText(data,pstr);
- }
- }
-
-
-
-
-
- void BaseDialog::SetItemTextf(short item,char *format,...)
- {
- va_list args;
- char text[256];
-
-
- // Set the text of a static text item or an edit
- // text item from a printf formatted specification
- va_start(args,format);
- vsprintf(text,format,args);
- va_end(args);
-
- SetItemText(item,text);
- }
-
-
-
-
-
- void BaseDialog::AppendItemText(short item,char *text)
- {
- char cur[256];
-
-
- // Append text to the end of a static text
- // item or an edit text item from a c string
- GetItemText(item,cur);
- strcat(cur,text);
- SetItemText(item,cur);
- }
-
-
-
-
-
- void BaseDialog::AppendItemTextf(short item,char *format,...)
- {
- va_list args;
- char cur[256],text[256];
-
-
- // Append text to the end of a static text item or an
- // edit text item from a printf formatted specification
- va_start(args,format);
- vsprintf(text,format,args);
- va_end(args);
-
- GetItemText(item,cur);
- strcat(cur,text);
- SetItemText(item,cur);
- }
-